package com.fruit.util;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import org.apache.commons.lang3.StringUtils;

/**
 * RequestHandler
 * @author Administrator 说明：日期处理 创建人：FH Q313596790
 * @version 2017.12.01 11:40
 */
public class RequestHandler {

	private String key;

	private Map<String, String> parameters;

	/**
	 * RequestHandler
	 * @author Administrator 说明：日期处理 创建人：FH Q313596790
	 * @version 2017.12.01 11:40
	 */
	public RequestHandler() {
		parameters = new TreeMap<String, String>();
	}

	/**
	 * setParameters
	 * @param ke key
	 * @param value value
	 * @author Administrator 说明：日期处理 创建人：FH Q313596790
	 * @version 2017.12.01 11:40
	 */
	public void setParameters(String ke, String value) {
		if (StringUtils.isNoneEmpty(ke)) {
			parameters.put(ke, value);
		}
	}

	/**
	 * createSign
	 * @author Administrator 说明：日期处理 创建人：FH Q313596790
	 * @version 2017.12.01 11:40
	 */
	public void createSign() {
		StringBuffer sb = new StringBuffer();
		Set<Entry<String, String>> es = parameters.entrySet();
		Iterator<Entry<String, String>> it = es.iterator();
		while (it.hasNext()) {
			Map.Entry<String, String> entry = it.next();
			String k = entry.getKey();
			String v = entry.getValue();
			if (StringUtils.isNotEmpty(v) && !"sign".equals(k) && !"key".equals(k)) {
				sb.append(k).append("=").append(v).append("&");
			}
		}
		sb.append("key=").append(key);
		String sign = MD5Util.MD5Encode(sb.toString(), "UTF-8").toUpperCase();

		this.setParameters("sign", sign);

	}

	/**
	 * createSign
	 * @param params params
	 * @return String
	 * @author Administrator 说明：日期处理 创建人：FH Q313596790
	 * @version 2017.12.01 11:40
	 */
	public String createSign(Map<String, String> params) {
		StringBuffer sb = new StringBuffer();
		Set<Entry<String, String>> es = params.entrySet();
		Iterator<Entry<String, String>> it = es.iterator();
		while (it.hasNext()) {
			Map.Entry<String, String> entry = it.next();
			String k = entry.getKey();
			String v = entry.getValue();
			if (StringUtils.isNotEmpty(v) && !"sign".equals(k) && !"key".equals(k)) {
				sb.append(k).append("=").append(v).append("&");
			}
		}
		sb.append("key=").append(key);
		String sign = MD5Util.MD5Encode(sb.toString(), "UTF-8").toUpperCase();
		return sign;
	}

	/**
	 * parseXML
	 * @return String
	 * @author Administrator 说明：日期处理 创建人：FH Q313596790
	 * @version 2017.12.01 11:40
	 */
	public String parseXML() {
		createSign();
		return CommonUtil.map2xmlBody(parameters, "xml");
	}

	/**
	 * getKey
	 * @return String
	 * @author Administrator 说明：日期处理 创建人：FH Q313596790
	 * @version 2017.12.01 11:40
	 */
	public String getKey() {
		return key;
	}

	/**
	 * setKey
	 * @param ke ke
	 * @author Administrator 说明：日期处理 创建人：FH Q313596790
	 * @version 2017.12.01 11:40
	 */
	public void setKey(String ke) {
		this.key = ke;
	}

}